
Imports System
Imports System.Collections
Imports System.Data.SqlClient
Imports _2bMatesSite.Common

Namespace _2bMatesSite.BO
	''' <summary>
	''' FavoriteGroups - base class for a user's list of groups.
	''' </summary>
	Public Class FavoriteGroups
		Inherits BaseList
		Protected userID As Integer

		Public Sub New(ByVal usr As User)
			Init(usr.ID)
		End Sub

		Public Sub New(ByVal userID As Integer)
			Init(userID)
		End Sub


		'
		' Private and protected methods
		'

		Protected Sub Init(ByVal userID As Integer)
			Me.rnd = New Random()
			Me.userID = userID
			Me.list = Nothing
		End Sub

		Protected Overloads Overrides Function LoadList() As Hashtable
			Return LoadList(userID)
		End Function

		Protected Function LoadList(ByVal userID As Integer) As Hashtable
			Dim favList As New Hashtable()

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Try
				Dim cmd As New SqlCommand(GetSelectSQL())
				cmd.Connection = conn
				cmd.Parameters.Add("@UID", userID)

				Dim dr As SqlDataReader = cmd.ExecuteReader()

				Dim ordID As Integer = dr.GetOrdinal("ID")
				While dr.Read()
					Dim fav As New FavoriteGroup(dr.GetInt32(ordID))
					favList.Add(fav.GetHashCode(), fav)
				End While
				dr.Close()
			Finally
				conn.Close()
			End Try

			Return favList
		End Function

		Protected Overloads Overrides Sub ClearList()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Try
				Dim cmd As New SqlCommand("DELETE FROM FavoriteGroups WHERE UID=@UID")
				cmd.Connection = conn
				cmd.Parameters.Add("@UID", userID)

				cmd.ExecuteNonQuery()
			Finally
				conn.Close()
			End Try
		End Sub

		Protected Overloads Overrides Sub DeleteItem(ByVal item As Object)
			Dim fav As FavoriteGroup = DirectCast(item, FavoriteGroup)
			DeleteItem(fav.ID)
		End Sub

		Protected Overloads Overrides Sub DeleteItem(ByVal favGID As Integer)
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Try
				Dim cmd As New SqlCommand("DELETE FROM FavoriteGroups WHERE UID=@UID AND FavoriteGroupID=@FavoriteGroupID")
				cmd.Connection = conn
				cmd.Parameters.Add("@UID", userID)
				cmd.Parameters.Add("@FavoriteGroupID", favGID)

				cmd.ExecuteNonQuery()
			Finally
				conn.Close()
			End Try
		End Sub

		Protected Overloads Overrides Function AddItem(ByVal item As Object) As Boolean
			Dim fav As New FavoriteGroup((DirectCast(item, Group)).ID)
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Try
				Dim cmd As New SqlCommand("INSERT INTO FavoriteGroups (UID, FavoriteGroupID) VALUES (@UID, @FavoriteGroupID)")
				cmd.Connection = conn
				cmd.Parameters.Add("@UID", userID)
				cmd.Parameters.Add("@FavoriteGroupID", fav.ID)

				cmd.ExecuteNonQuery()
			Finally
				conn.Close()
			End Try

			Return True
		End Function

		Protected Function GetSelectSQL() As String
			Dim sql As String = "SELECT grp.* FROM" + Environment.NewLine
			sql += "" & Chr(9) & "FavoriteGroups fav," + Environment.NewLine
			sql += "" & Chr(9) & "Groups grp" + Environment.NewLine
			sql += "WHERE" + Environment.NewLine
			sql += "" & Chr(9) & "fav.UID=@UID" + Environment.NewLine
			sql += "AND" & Chr(9) & "fav.FavoriteGroupID = grp.ID" + Environment.NewLine
			Return sql
		End Function
	End Class
End Namespace






